package com.hxkj.cst.chengshuitong.ui.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.hxkj.cst.chengshuitong.R
import com.hxkj.cst.chengshuitong.base.BaseActivity
import com.hxkj.cst.chengshuitong.closeInputMethod
import com.hxkj.cst.chengshuitong.control.LoginControl
import com.hxkj.cst.chengshuitong.model.ServiceResultModel
import com.hxkj.cst.chengshuitong.presenter.LoginPresenter
import com.hxkj.cst.chengshuitong.toast
import kotlinx.android.synthetic.main.activity_login.*

/**
 * 作者： Luoyang on 2017/5/26.
 * 描述：
 */
class LoginActivity : BaseActivity<LoginPresenter>(), LoginControl.View {
    override fun provideContentView(): Int = R.layout.activity_login

    override fun createPresenter(): LoginPresenter? = LoginPresenter(this, this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setListener()
    }

    fun setListener() {
        al_btn_login.setOnClickListener { login() }
        al_tv_register.setOnClickListener { startActivity(Intent(this, RegisterActivity::class.java)) }
        al_tv_forget.setOnClickListener { startActivity(Intent(this, ForgetActivity::class.java)) }
    }

    fun login() {
        closeInputMethod(al_btn_login.windowToken)

        val phone = al_ci_user.getText()
        val password = al_ci_password.getText()

        if (TextUtils.isEmpty(phone)) toast("用户名不能为空！")
        else if (TextUtils.isEmpty(password)) toast("密码不能为空！")
        else mPresenter?.login(phone, password)
    }

    override fun loginResult(result: String) {
        if (result == ServiceResultModel.SUCCESS) {
            toast("登陆成功！")
            finish()
        }

        if (result == ServiceResultModel.FAILED) {
            toast("登陆失败，请检查用户名或密码是否正确")
        }

    }


}